package com.prs.crm.action.sys;

import java.io.File;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.sys.CachetMaintain;
import com.prs.crm.service.sys.CachetMaintainService;

public class CachetMaintainAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private Integer[] ids;
	private PaginationSupport pagination;
	private CachetMaintain cachetMaintain;
	private CachetMaintainService cachetMaintainService;
	private File upload;
	private String uploadFileName;
	
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public CachetMaintain getCachetMaintain() {
		return cachetMaintain;
	}
	public void setCachetMaintain(CachetMaintain cachetMaintain) {
		this.cachetMaintain = cachetMaintain;
	}
	public CachetMaintainService getCachetMaintainService() {
		return cachetMaintainService;
	}
	public void setCachetMaintainService(CachetMaintainService cachetMaintainService) {
		this.cachetMaintainService = cachetMaintainService;
	}
	
	@Action("listCachetMaintain")
	public String listCachetMaintain(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(CachetMaintain.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCachetMaintainService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editCachetMaintain")
	public String editCachetMaintain(){
		if(ids!=null){
			this.cachetMaintain=this.getCachetMaintainService().get(ids[0]);
		}
		return SUCCESS;
	}
	@Action(value="saveCachetMaintain",results={
			@Result(name="success",type="redirect",location="listCachetMaintain"),
			@Result(name="error",type="chain",location="editCachetMaintain")
	})
	public String saveCachetMaintain(){
		if (cachetMaintain.getName()==null|| "".equals(cachetMaintain.getName().trim())) {
			this.addActionError("印章名不能为空！");
			return ERROR;
		}
		if (upload!=null) {
			this.cachetMaintain.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
			if (cachetMaintain.getAttach().getContentType()!="esp") {
				this.addActionError("请上传esp文件!");
				return ERROR;
			}
		}else{
			this.addActionError("请上传印章！");
			return ERROR;
		}
		this.getCachetMaintainService().save(cachetMaintain);
		return SUCCESS;
	}
	@Action(value="removeCachetMaintain",results={
			@Result(name="success",type="redirect",location="listCachetMaintain"),
			@Result(name="error",type="chain",location="listCachetMaintain")
	})
	public String removeCachetMaintain(){
		if(ids!=null){
			this.cachetMaintain=this.getCachetMaintainService().get(ids[0]);
		}
		this.getCachetMaintainService().batchRemove(ids);
		return SUCCESS;
	}
}
